Date.prototype.format = function(format) {
  /*
   * eg:format="YYYY-MM-dd hh:mm:ss";

   */
  var o = {
      "M+" :this.getMonth() + 1, // month
      "d+" :this.getDate(), // day
      "h+" :this.getHours(), // hour
      "m+" :this.getMinutes(), // minute
      "s+" :this.getSeconds(), // second
      "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
      "S" :this.getMilliseconds()
  // millisecond
  }
  if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear() + "")
              .substr(4 - RegExp.$1.length));
  }
  for ( var k in o) {
      if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                  : ("00" + o[k]).substr(("" + o[k]).length));
      }
  }
  return format;
}

class TSeed{
  constructor(){
    this.config={
      sysTitle:"粮库害虫诱捕系统",
      version:"v2022",
      copyRight:"中科芯禾(深圳)科技有限公司",
      logoUrl:"https://ai.seedlight.cn:31000/iot-web-manage/img/home-logo.8be66afc.svg",
      apiUrl:"https://ai.seedlight.cn:31000/foo/seedlight/commonapp",
      devControlUrl:"https://ai.seedlight.cn:31000/foo/seedlight/",
      deviceApi:"https://ai.seedlight.cn:31000/foo/seedlight/getAppAllRealData"
    };
    this.userInfo={};
    this.customs=[
      {
        customId:1, customCode:"c01",customName:"中储粮XX管理集团"
      },
      {
        customId:2, customCode:"c02",customName:"西北粮食管理集团"
      },
      {
        customId:3, customCode:"c03",customName:"测试公司"
      },
    ];
    this.grains=[
      {
        grainId:1, grainCode:"c01",grainName:"1#仓"
      },
      {
        grainId:2, grainCode:"c02",grainName:"2#仓"
      },
      {
        grainId:3, grainCode:"c03",grainName:"3#仓"
      },
    ]
  };
  callApi(apiCode , apiPs){
    var me = this;
    return new Promise((success, faild)=>{
      try {
        var psData = apiPs||{};
        psData.code = apiCode;
        wx.request({
          method:"POST",
          url:me.config.apiUrl,
          data:psData,
          complete:(res)=>{
            var result = (res && res.data && res.data.body)?res.data.body:null;
            success(result);
          }
        })
      } catch (error) {
        console.log('call api error', error);
      }
    })
  };
  callDeviceApi(apiCode , apiPs){
    var me = this;
    return new Promise((success, faild)=>{
      try {
        var psData = apiPs||{};
        psData.code = apiCode;
        wx.request({
          method:"POST",
          url:me.config.devControlUrl+apiCode,
          data:psData,
          complete:(res)=>{
            var result = (res && res.data && res.data.body)?res.data.body:null;
            success(result);
          }
        })
      } catch (error) {
        console.log('call api error', error);
      }
    })
  }
  getDeviceDatas(queryType , key){
    var me = this;
    var deviceDatas={};
    return new Promise((success, faild)=>{
      try {
        var codes ={
          g:'device/getDeviceListByGrainId' ,
          b:'device/getDeviceListBybarnId' ,
          d:'device/getDeviceListBySnCode'
        }
        var keys={
          g:"grainId" ,
          b:"barnId",
          d:"snCode"
        }
        var psData = {
          code: codes[queryType],
          grainId:0,
          barnId:0,
          snCode:""
        };
        psData[keys[queryType]] = key;
        wx.request({
          method:"POST",
          url:me.config.deviceApi,
          data:psData,
          complete:(res)=>{
            var devs = (res && res.data && res.data.body)?res.data.body:[];
            devs.forEach(d=>{
              deviceDatas[d.snCode] = d;
              d.v= (d.volt*0.001).toFixed(2);
              d.i= (d.cur*0.001).toFixed(2);
              d.photoTm='';
              try {
                var tmpArr = d.imageUrl.split('_');
                var tmpArr1= (tmpArr[tmpArr.length-1]).split('.')
                var tm = parseInt( 1000 * tmpArr1[0]);
                var dt = new Date(tm);
                d.photoTm = dt.format("yyyy-MM-dd hh:mm:ss");
                console.log(d)
              } catch (error1) {
                console.log(error1);
              }
            })
            success(deviceDatas);
          }
        })
      } catch (error) {
        console.log('call device api error', error);
        success(deviceDatas);
      }
    })
  }; 
}
export const seed = new TSeed();